from vec import Vec2
import math
WIDTH = 500
HEIGHT = 500

PEN_COLOR = (0, 200, 0)
BACK_COLOR = (0, 0, 0)

def get_pt_by_len(pt_mid, n,Dir):
    y_offset = n / math.sqrt(3) 
    pt_a = Vec2(pt_mid.x, pt_mid.y - y_offset)
    pt_d = Vec2(pt_mid.x, pt_mid.y + n / 2 / math.sqrt(3))
    pt_b = Vec2(pt_d.x - n / 2, pt_d.y)
    pt_c = Vec2(pt_d.x + n / 2, pt_d.y)
    pt_a-=Dir
    pt_b+=Dir
#    pt_c-=Dir

    return pt_a, pt_b, pt_c,y_offset

pt_mid = Vec2(250, 250)
long=100
pt_a,pt_b,pt_c,h=get_pt_by_len(pt_mid,long,Vec2(0,0))
pt_list=[pt_a,pt_b,pt_c]
got_list=[]

def get_pos(a,b,p):
    return a*(1-p)+b*p

def create_tri(ae,be,num):
    global got_list,long,h
    do=get_pos(ae,be,0.5)
    hl=Vec2(be.y-be.x,be.x-be.y)
    hl.normalize()
    hl*=h/3/2
    hl+=do
    be.normalize()
    be.x*=do.x
    be.y*=do.y
    a,b,c,d=get_pt_by_len(hl,long/3,be)
    if num==1:
        g=[]
        g.append(c)
        g.append(b)
        g.append(a)
        got_list.append(g)
    elif num==2:
        g=[]
        g.append(b)
        g.append(a)
        g.append(c)
        got_list.append(g)
    else:
        g=[]
        g.append(a)
        g.append(c)
        g.append(b)
        got_list.append(g)

create_tri(pt_b,pt_a,1)
create_tri(pt_a,pt_c,2)
create_tri(pt_c,pt_b,3)
"""
j=0
for i in range(0,len(got_list),3):
    pt_list.insert(j,got_list[i])
    pt_list.insert(j+1,got_list[i+1])
    pt_list.insert(j+2,got_list[i+2])
    j+=4
"""
def draw():
    global pt_list
    screen.fill(BACK_COLOR)
    j=0
    for i in pt_list:
        if not j==len(pt_list)-1:
            j+=1
        else:
            break
        screen.draw.line(i.raw_get(),pt_list[j].raw_get(),PEN_COLOR)
    j=0
    for i in got_list:
        for k in i:
            if not j==len(i)-1:
                j+=1
            else:
                break
            screen.draw.line(k.raw_get(), i[j].raw_get(), PEN_COLOR)
        j=0
        screen.draw.line(i[-1].raw_get(), i[0].raw_get(), PEN_COLOR)

    screen.draw.line(pt_list[-1].raw_get(), pt_list[0].raw_get(), PEN_COLOR)
#    screen.draw.line(got_list[-1].raw_get(), got_list[0].raw_get(), PEN_COLOR)